﻿using EdiNet;
using Xunit;
using System;

namespace EdiNet.Test
{
    
    public class DataSegmentTest
    {


        /// <summary>
        ///A test for DataSegment Constructor
        ///</summary>
        [Fact()]
        public void DataSegmentConstructorTest()
        {
            string segId = "BEG";
            DataSegment target = new DataSegment(segId);
            Assert.Equal(segId, target.SegId);
            Assert.Equal(target.Count(), 1);
        }

        /// <summary>
        ///A test for Add
        ///</summary>
        [Fact()]
        public void AddTest()
        {
            string segId = "BEG"; 
            DataSegment target = new DataSegment(segId); 
            string elementValue = "00"; 
            DataElement el = target.Add(elementValue);
            Assert.Equal(target.Count(), 2);
            Assert.Equal(el.V, elementValue);
        }

        /// <summary>
        ///A test for Count
        ///</summary>
        [Fact()]
        public void CountTest()
        {
            string segId = "BEG"; 
            DataSegment target = new DataSegment(segId);
            target.Add("AA");
            target.Add("BB");
            int expected = 3; 
            int actual;
            actual = target.Count();
            Assert.Equal(expected, actual);
        }

        /// <summary>
        ///A test for ToString
        ///</summary>
        [Fact()]
        public void ToStringTest()
        {
            string segId = "BEG"; 
            DataSegment target = new DataSegment(segId);
            target.Add("AA");
            target.Add("BB").Add("CC");
            char dataElementSeparator = '`'; 
            char componentElementSeparator = '/'; 
            string expected = "BEG`AA`BB/CC"; 
            string actual;
            actual = target.ToString(dataElementSeparator, componentElementSeparator);
            Assert.Equal(expected, actual);
        }

        /// <summary>
        ///A test for ToString
        ///</summary>
        [Fact()]
        public void ToStringTest1()
        {
            string segId = "BEG";
            DataSegment target = new DataSegment(segId);
            target.Add("AA");
            target.Add("BB").Add("CC");
            char dataElementSeparator = '`';
            char componentSeparator = '|';
            string expected = "BEG`AA`BB|CC";
            string actual;
            actual = target.ToString(dataElementSeparator, componentSeparator);
            Assert.Equal(expected, actual);
        }

        /// <summary>
        ///A test for Item
        ///</summary>
        [Fact()]
        public void ItemTest()
        {
            string segId = "BEG"; 
            DataSegment target = new DataSegment(segId); 
            int position = 2; 
            string val = "1234";
            target[position].V = val;
            DataElement actual;
            actual = target[position];
            Assert.Equal(target[0], segId);
            Assert.Equal(target[1], string.Empty);
            Assert.Equal(val, actual);
            Assert.Equal(target.Count(), 3);
        }

        /// <summary>
        ///A test for Item, getter actually creates elements
        ///</summary>
        [Fact()]
        public void ItemTest2()
        {
            string segId = "BEG";
            DataSegment target = new DataSegment(segId);
            Assert.Equal(target[20], string.Empty);
            Assert.Equal(target.Count(), 21);
        }


        /// <summary>
        ///A test for FormatIsaSegment
        ///</summary>
        [Fact()]
        public void FormatIsaSegmentTest()
        {
            DataSegment isa = new DataSegment("ISA");
            DataSegment.FormatIsaSegment(isa);
            Assert.Equal(isa[1], "00");
            Assert.Equal(isa[2], "          ");
            Assert.Equal(isa[3], "00");
            Assert.Equal(isa[4], "          ");
            Assert.Equal(isa[5], "  ");
            Assert.Equal(isa[6], "               ");
            Assert.Equal(isa[7], "  ");
            Assert.Equal(isa[8], "               ");
            Assert.Equal(isa[9], "000000");
            Assert.Equal(isa[10], "0000");
            Assert.Equal(isa[11], "U");
            Assert.Equal(isa[12], "00000");
            Assert.Equal(isa[13], "000000000");
            Assert.Equal(isa[14], "0");
            Assert.Equal(isa[15], "T");
            Assert.Equal(isa[16], EdiReaderWriterSettings.DefaultX12.ComponentSeparatorChar.ToString());
        }

        /// <summary>
        ///A test for GetHashCode
        ///</summary>
        [Fact()]
        public void GetHashCodeTest()
        {
            DataSegment s1 = new DataSegment("AAA");
            DataSegment s2 = new DataSegment("AAA");
            Assert.Equal(true, s1.GetHashCode() == s2.GetHashCode());
        }

        /// <summary>
        ///A test for Equals
        ///</summary>
        [Fact()]
        public void EqualsTest()
        {
            DataSegment target = new DataSegment("AAA");
            object obj1 = new DataSegment("AAA");
            object obj2 = new DataSegment("BBB");
            object obj3 = target;

            Assert.Equal(true, target.Equals(obj1));
            Assert.Equal(false, target.Equals(obj2));
            Assert.Equal(true, target.Equals(obj3));
            Assert.Equal(false, target.Equals(null));
        }

        /// <summary>
        ///A test for op_Equality
        ///</summary>
        [Fact()]
        public void op_EqualityTest()
        {
            DataSegment el1 = new DataSegment("AAA");
            DataSegment el1a = el1;
            DataSegment el2 = new DataSegment("BBB");
            DataSegment el3 = new DataSegment("AAA");

            Assert.Equal(false, el1 == el2);
            Assert.Equal(false, null == el2);
            Assert.Equal(false, el1 == null);
            Assert.Equal(true, el1 == el3);
            Assert.Equal(true, el1a == el1);
        }

        /// <summary>
        ///A test for op_Inequality
        ///</summary>
        [Fact()]
        public void op_InequalityTest()
        {
            DataSegment el1 = new DataSegment("AAA");
            DataSegment el1a = el1;
            DataSegment el2 = new DataSegment("BBB");
            DataSegment el3 = new DataSegment("AAA");

            Assert.Equal(true, el1 != el2);
            Assert.Equal(true, null != el2);
            Assert.Equal(true, el1 != null);
            Assert.Equal(false, el1 != el3);
            Assert.Equal(false, el1a != el1);
        }
    }
}
